<!--用户管理-->
<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" ref="SuperTablePage">


      <template v-slot:download_slot="{ row }">

        <div v-for="item, index in row.downloadFiles"
          style="display: flex;  align-content: center; align-items: center; gap: 10px;">
          <div style="width: 100px; font-weight: bold; text-align: left; ">{{ item.type }}</div>
          <div style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden; flex: 0.7; ">
            {{ item.url }}
          </div>
          <el-button type="text" icon="el-icon-document-copy" @click="$copyToClip(item.url, '下载地址已复制')"
            style="margin-left: 20px;">{{ $tt('label_copy_address') }}</el-button>
          <el-button type="text" icon="el-icon-download" @click="openUrl(item.url)" style="margin-left: 20px;"> {{
      $tt('label_download') }} </el-button>

        </div>


      </template>

    </ex_super_table_page>
    <ex_super_dialog_form ref="formDialog" v-bind="dialog" @afterSubmitSuccess="reload()">
    </ex_super_dialog_form>



  </div>
</template>
<script>

let moduleName = "app 版本管理"

import SuperTablePageFunction from '@/mixin/super-table-page-function';
const defaultObj = {
  "mustUpdate": "Y",
  status: "DISABLED",
  files: [],
}
export default {
  title: moduleName,
  components: {},

  mixins: [SuperTablePageFunction],
  data() {

    return {
      selection: [],


      dialog: {
        customClass: 'dialog_auto',
        submitUrl: '/oms-api/base/OmsAppUpdateRpc/uploadNewVersion.json',
        submitDataHandle: (data) => {
          delete data.isUpload
          return data;
        },
        form: {
          itemClass: "form_item_default", inputClass: "form_input_big",
          model: [
            {
              key: 'appType', label: 'label_app_type', type: 'radio-group', enum: 'AppTypeEnum', border: true,
              disabled: (data) => !_.isEmpty(data.id)
            },
            { key: 'version', label: 'label_app_version_code', },
            { key: 'description', label: 'label_update_description', },

            { key: 'mustUpdate', label: 'label_app_must_update', type: 'radio-group', enum: 'YesOrNoEnum', border: true, },
            { key: 'status', label: 'label_status', type: 'radio-group', enum: 'IsAvailableStatusEnum', border: true, },

            {
              key: 'downloadPath', label: 'label_app_upload',
              render: (h, { data, item }) => {
                return (<ex_super_upload value={data['downloadPath']} type='file' limit={1}
                  onInput={e => {
                    this.$set(data, 'downloadPath', e);
                  }}
                  accept=".zip,.apk,.exe"
                  maxSize={200}
                  onIsUploadChange={e => this.$set(data, 'isUpload', e)}
                  headers={{ targetFolder: '_public_', 'keepFileName': 'Y' }}
                />)
              }
            },
            { key: 'isUpload', label: '', render: (h, { }) => null, },
          ],
          rules: {
            'appType': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'version': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'mustUpdate': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'status': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'downloadPath': [{ required: true, type: "array", message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            isUpload: {
              validator: (r, v, cb) => {
                if (v) {
                  cb(this.$tt("label_uploading"))
                } else {
                  cb()
                }
              }
            },

          },
        }
      },
      option: {

        action: [
          {
            label: 'label_app_upload', attrs: { icon: "el-icon-plus" }, click: () => {

              this.$refs.formDialog.showNew({
                title: this.$t("label_add_app_version"),
                data: _.clone(defaultObj),
              })
            },
            if: () => !this.justDownload
          }
        ],

        table: {
          autoPage: false,
          url: !this.justDownload ? "/oms-api/base/OmsAppUpdateRpc/query.json" : '/wms-api/base/WmsAppUpdateRpc/getLatestApps.json',
          attrs: {
            'cell-style': ({ row, column, rowIndex, columnIndex }) => {
              if (column.property == 'status') {
                return { color: row.status == 'NORMAL' ? this.$globalCss.successColor : this.$globalCss.infoColor }
              }
            }
          },
          model: [
            {
              key: 'appType', label: 'label_app_name', align: 'left', 'min-width': 200,
              attrs: {
                formatter: (r, c, v, i) => '[' + v + '] ' + this.$enumShow(r.appType, 'AppTypeEnum')
              }
            },
            {
              key: 'version', label: 'label_app_version', align: 'left', 'min-width': 120,
            },
            {
              key: 'description', label: 'label_description', align: 'left', 'min-width': 120,
            },
            {
              key: 'mustUpdate', label: 'label_app_must_update', align: 'left', 'min-width': 120,
            },
            {
              key: 'status', label: 'label_status', align: 'left', 'min-width': 120,
              attrs: {
                formatter: (r, c, v, i) => this.$enumShow(r.status, 'IsAvailableStatusEnum')
              }
            },
            {
              key: 'downloadPath', label: 'label_download_address', align: 'center', 'min-width': 400,
              slot: 'download_slot'
            },

            {
              if: () => !this.justDownload,
              type: 'operate', width: 220,
              operate: [
                {
                  label: 'label_edit', click: (row) => {
                    this.$refs.formDialog.showNew({
                      title: this.$t("label_edit_app_version"),
                      data: Object.assign(_.clone(defaultObj), row),
                    })
                  }
                }
              ],
            }

          ]
        }
      },

    };
  },

  props: {
    justDownload: {
      type: Boolean,
      default: false
    }
  },

  watch: {

  },

  created() {



  },


  destroyed() {

  },

  methods: {

    openUrl(url) {
      window.open(url)
    },
    submitForm() {

      if (!this.formValue.downloadPath || this.formValue.downloadPath.length == 0) {
        this.$message({
          showClose: true,
          message: '请上传附件',
          type: 'error'
        })
        return
      }

      this.$refs['formEl'].validate((valid) => {
        if (valid) {

          if (this.viewType == 'bill') {
            this.formValue.billId = this.billId
          }

          this.$ajax({
            url: '/oms-api/base/OmsAppUpdateRpc/uploadNewVersion.json',
            type: 'json',
            data: this.formValue,
            success: (res) => {
              this.$message({
                showClose: true,
                message: '提交成功',
                type: 'success'
              })
              this.$refs['SuperTablePage'].reload()
              this.dialogFormVisible = false
            }
          });

        } else {
          return false;
        }

      });

    }


  }
};

</script>
<style></style>
